package taskthree;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

class Point {
    private double[] coordinates;
    Point[] neighbors;

    public Point(int dimensions) {
        coordinates = new double[dimensions];
    }

    public void setCoordinates(double[] coords) {
        this.coordinates = coords;
    }

    public double[] getCoordinates() {
        return coordinates;
    }

    public double distanceTo(Point other) {
        double sum = 0.0;
        for (int i = 0; i < coordinates.length; i++) {
            sum += Math.pow(this.coordinates[i] - other.coordinates[i], 2);
        }
        return Math.sqrt(sum);
    }

    public void setNeighbors(Point[] neighbors) {
        this.neighbors = neighbors;
    }

    public Point[] getNeighbors() {
        return neighbors;
    }
}

public class  taskthree{

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 获取用户输入
        System.out.println("Please enter the number of points:");
        int M = scanner.nextInt();
        System.out.println("Please enter the number of dimensions:");
        int N = scanner.nextInt();
        System.out.println("Please enter the number of neighbors:");
        int k = scanner.nextInt();

        // 创建点的数组
        Point[] points = new Point[M];

        // 随机生成点
        for (int i = 0; i < M; i++) {
            points[i] = new Point(N);
            double[] features = new double[N];
            Random random1 = new Random((i + 500) * 6);
            for (int j = 0; j < N; j++) {
                features[j] = random1.nextDouble();
            }
            points[i].setCoordinates(features);
        }

        // 寻找最近的两个点
        int minX = -1, minY = -1;
        double minDistance = Double.MAX_VALUE;

        for (int i = 0; i < M; i++) {
            for (int j = i + 1; j < M; j++) {
                double distance = points[i].distanceTo(points[j]);
                if (distance < minDistance) {
                    minDistance = distance;
                    minX = i;
                    minY = j;
                }
            }
        }

        System.out.print("The Nearest two points are: " + minX + " and " + minY);

        // 寻找每个点的 k 个最近邻
        for (int i = 0; i < M; i++) {
            Point[] neighbors = new Point[k];
            double[] distances = new double[M];
            for (int j = 0; j < M; j++) {
                distances[j] = points[i].distanceTo(points[j]);
            }
            Integer[] indices = new Integer[M];
            for (int j = 0; j < M; j++) {
                indices[j] = j;
            }

            Arrays.sort(indices, (a, b) -> Double.compare(distances[a], distances[b]));

            for (int j = 0; j < k; j++) {
                neighbors[j] = points[indices[j + 1]]; // indices[0] is the point itself, so skip it
            }

            points[i].setNeighbors(neighbors);
            System.out.print("\nThe " + i + "th Point's " + k + " Nearest Neighbors are: ");
            for (int j = 0; j < k; j++) {
                System.out.print(indices[j + 1] + " ");
            }
        }

        scanner.close();
    }
}
